home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / N-P / NShell-Pro 1.50.sit / nShell-Pro / doc / nShell™ User's Guide.rsrc / TEXT_134.txt < prev    next >
Encoding:
Text File  |  1994-12-27  |  3.7 KB  |  116 lines

  1.  
  2. Input/Output Redirection
  3.  
  4. One of the principal strengths of shell environments, is the way that several small commands can be linked together to provide greater functionality.  Input/Output Redirection is the process by which a user may direct the input or output of a command to a file, or even another command. 
  5.  
  6. Straight Pipes
  7.  
  8. Straight pipes take the output of one command and pass it to another. The second command, rather than looking for input from the keyboard, takes input from the first command.  An example of a simple pipe would be:
  9.  
  10.     hello | notify
  11.  
  12. The "|" character is used to specify a pipe.  In this example the hello command produces the string "Hello World", which is then sent to the notify command.  The notify command pops a dialog containing the "Hello World" string.
  13.  
  14. A longer series of commands could be piped together:
  15.  
  16.     hello | wc | notify
  17.  
  18. In this case the "Hello World" string is sent to wc, a word count command, and the result of wc is sent to notify.
  19.  
  20. Redirecting To And From Files
  21.  
  22. The inputs and outputs of commands can be directed to files.  For example, the command:
  23.  
  24.     hello >hello.txt
  25.  
  26. directs the output of the hello command to a file called hello.txt.
  27.  
  28. In a similar way, inputs to commands can be directed to files.  For example, the command:
  29.  
  30.     wc <hello.txt
  31.  
  32. directs the wc command to take its input from the file called hello.txt.
  33.  
  34. Input and output may be redirected at the same time, the command:
  35.  
  36.     wc <hello.txt >wc.txt
  37.  
  38. directs the wc command to take its input from hello.txt and to write its output to wc.txt.
  39.  
  40. Adding To Existing Files
  41.  
  42. The '>' redirector creates a new file to hold the command's output.  If a file exists, it will be overwritten.  The '>>' redirector tells the shell to "add to" the existing file:
  43.  
  44.     hello >>hello.txt
  45.  
  46. Standard Error
  47.  
  48. To prevent errors from being hidden, the nShell splits output into two streams "Standard Output" and "Standard Error".  Consider the example:
  49.  
  50.     % man >man.txt
  51.     Usage: man <command or keyword>.
  52.  
  53. In this case an error message was generated and written to "Standard Error".  If we wanted to direct the error message to a file we would use the "&" character:
  54.  
  55.     man >&man.out
  56.  
  57. The "&" character can be used with the ">>" redirector also, as in:
  58.  
  59.     man >>&man.out
  60.  
  61. Special Devices
  62.  
  63. Two special output devices are available within the nShell:
  64.  
  65. dev:null    Discard output
  66. dev:tty     Redirect to the nShell window
  67.  
  68. The first device, dev:null, is useful when you wish to run a command, but are not interested in its output.  Consider the "chattr" command used to modify a file's creator or type:
  69.  
  70.     % chattr script.txt -c john
  71.     Crea   Type  Name
  72.     ------ ------ ----
  73.     'john' 'TEXT' script.txt
  74.  
  75. By redirecting the result of this command to dev:null, the output can be suppressed:
  76.  
  77.     % chattr script.txt -c john >dev:null
  78.  
  79. The second device, dev:tty, is useful when overriding a previous output redirection.  Consider the script:
  80.  
  81.     echo "hello one"
  82.     echo "hello two" >dev:tty
  83.     echo "hello three"
  84.  
  85. If no redirection is performed, the output of this script would be:
  86.  
  87.     % script
  88.     hello one
  89.     hello two
  90.     hello three
  91.  
  92. If we were to try redirecting the output to a file, the second line would still appear on the console:
  93.  
  94.     % script >script.txt
  95.     hello two
  96.  
  97. And the file "script.txt" would contain:
  98.  
  99.     hello one
  100.     hello three
  101.  
  102. Use dev:tty when you want to make sure that a message goes to the console.
  103.  
  104. Protecting Characters
  105.  
  106. As you can see above, the shell assumes that ">" and "<" characters are requesting redirection.  This could result in some strange effects.  The command
  107.  
  108.     notify <oops!>
  109.  
  110. would mean take input from a file called "oops!>".  (The input filename is terminated by the first space.).
  111.  
  112. Protecting redirection characters with quotes:
  113.  
  114.     notify "<oops!>"
  115.  
  116. would have the desired results.